import { Plugin, Context } from "@nuxt/types";
import { Inject } from "@nuxt/types/app";
import requestInit from "@/request";

type $aip = {[key: string]: <T>(data?: {url: string; data?: {[key: string]: any}; [key: string]: any}) => Promise<T>}

declare module 'vue/types/vue' {
  interface Vue {
    $api: $aip
  }
}

declare module '@nuxt/types' {
  interface NuxtAppOptions {
    $api: $aip
  }
  // nuxtContext.$myInjectedFunction
  interface Context {
    $api: $aip
  }
}

declare module 'vuex/types/index' {
  interface Store<S> {
    $api: $aip
  }
}

const router: Plugin = (context: Context, inject: Inject) => {
  const { $axios } = context

  inject("api", requestInit($axios))
}

export default router
